package com.task.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginHandlerInterceptor implements HandlerInterceptor {
	Logger logger = LoggerFactory.getLogger(LoginHandlerInterceptor.class);
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String basePath = request.getContextPath();
		String path = request.getRequestURI();
		logger.info("basePath:" + basePath);
		logger.info("path:" + path);
		
		Object user = request.getSession().getAttribute("user");
        // 如果获取的request的session中的loginUser参数为空（未登录），就返回登录页，否则放行访问
        if (user == null) {
            // 获取request返回页面到登录页
        	response.sendRedirect(request.getContextPath());
            return false;
        } else {
            // 已登录，放行
            return true;
        }
	}
}
